package com.xzakota.minesweeper.widget;

import com.xzakota.minesweeper.widget.text.InputTextField;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;

/**
 * 房间信息展示视图
 * 包括 房间号、在线人数、进入按钮
 *
 * @author Xzakota
 */
public class RoomInfoPanel extends JPanel {
    private final InputTextField roomLabel;
    private final JButton roomButton;
    private int onlineUsers = 0;
    private int maxUsers = 4;

    public RoomInfoPanel() {
        setLayout(new BoxLayout(this, BoxLayout.X_AXIS));

        roomLabel = new InputTextField();
        roomLabel.setTagText("房间: ");
        roomLabel.setFieldText(onlineUsers + "/" + maxUsers);
        roomLabel.setFieldEditable(false);
        roomLabel.setAllFont(new Font("黑体", Font.PLAIN, 18));
        add(roomLabel);

        add(Box.createHorizontalStrut(35));
        roomButton = new JButton("进入");
        roomButton.setFocusable(false);
        add(roomButton);
    }

    public void setRoomButtonActionListener(ActionListener actionListener) {
        for (ActionListener listener : roomButton.getActionListeners()) {
            roomButton.removeActionListener(listener);
        }

        roomButton.addActionListener(actionListener);
    }

    private void setUsersState() {
        roomLabel.setFieldText(onlineUsers + "/" + maxUsers);
    }

    public void setRoomLabel(String text) {
        roomLabel.setTagText(text);
    }

    public void setOnlineUsers(int onlineUsers) {
        this.onlineUsers = onlineUsers;
        setUsersState();
    }

    public void setMaxUsers(int maxUsers) {
        this.maxUsers = maxUsers;
        setUsersState();
    }
}
